﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       BaseCamera
- 机器名称：       YTE
- 命名空间：       LD.Vision.Camera
- 文 件 名：       BaseCamera
- 创建时间：       2022/7/13 18:36:47
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using HalconDotNet;
using HWindow_Tool.Control;
using LD.Host;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace LD.Vision
{
    public abstract class AbsCamera
    {
        public ManualResetEvent resetEvent = new ManualResetEvent(false);
        private string name = string.Empty;
        public int ScreenIndex { get; set; } = 0;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                if (!string.IsNullOrEmpty(value))
                {
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{name}");
                    string pathModel = Path.Combine(path, $@"Template");
                    string pathCal = Path.Combine(path, $@"Calibration");
                    string pathImage = Path.Combine(path, $@"Image");
                    //检查目录
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    if (!Directory.Exists(pathModel))
                        Directory.CreateDirectory(pathModel);
                    if (!Directory.Exists(pathCal))
                        Directory.CreateDirectory(pathCal);
                    if (!Directory.Exists(pathImage))
                        Directory.CreateDirectory(pathImage);

                    //模板
                    foreach (string item in Directory.GetFiles(pathModel, "*.ldm", SearchOption.TopDirectoryOnly))
                    {
                        string fileName = Path.GetFileNameWithoutExtension(item);//去掉后缀名
                        HShapeModel shapeModel = new HShapeModel();
                        shapeModel.ReadShapeModel(item);
                        ShapeModels.Add(fileName, shapeModel);
                    }
                    //标定
                    CalibDatas.Clear();
                    //单文件读取
                    string ldcFilePath = Path.Combine(pathCal, $"{name}.ldc");
                    if (File.Exists(ldcFilePath))
                    {
                        string fileName = Path.GetFileNameWithoutExtension(ldcFilePath);//去掉后缀名
                        var cal = JsonSerializeHelper.Load<CalibrationModel>(ldcFilePath);
                        CalibDatas.Add(cal.Name, cal);
                    }

                    //多文件读取
                    //foreach (string item in Directory.GetFiles(pathCal, "*.ldc", SearchOption.TopDirectoryOnly))
                    //{
                    //    var cal = SerializeHelper.Load<Calibration>(item);
                    //    CalibDatas.Add(cal.Name, cal);
                    //}
                }
            }
        }
        public void SaveShape(string name, HShapeModel shapeModel)
        {
            if (!ShapeModels.ContainsKey(name))
            {
                ShapeModels.Add(name, shapeModel);
            }
        }
        /// <summary>
        /// 模板
        /// </summary>
        public Dictionary<string, HShapeModel> ShapeModels { get; private set; } = new Dictionary<string, HShapeModel>();
        public Dictionary<string, CalibrationModel> CalibDatas { get; private set; } = new Dictionary<string, CalibrationModel>();
        /// <summary>
        /// 保存图片
        /// </summary>
        public bool IsSaveImage { get; set; }
        //private HWindow_Final _hWindow = new HWindow_Final();
        //public HWindow_Final HWindow
        //{
        //    get { return _hWindow; }
        //    set
        //    {
        //        _hWindow = value;
        //        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{name}");
        //        string pathModel = Path.Combine(path, $@"Model");
        //        value.viewWindow.loadROI(Path.Combine(pathModel, $"{name}.xml"), out Roi);
        //    }
        //}
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="index"></param>
        public virtual void Open(int index) { }
        /// <summary>
        /// 关闭相机
        /// </summary>
        public virtual void Close() { }
        /// <summary>
        /// 开始
        /// </summary>
        public virtual void StartGrab() { }
        /// <summary>
        /// 停止
        /// </summary>
        public virtual void StopGrab() { }
        /// <summary>
        /// 连续采集
        /// </summary>
        public virtual bool ContinuesMode { get; set; }
        /// <summary>
        /// 触发模式
        /// </summary>
        public virtual bool TriggerMode { get; set; }
        /// <summary>
        /// 软触发一次
        /// </summary>
        public virtual bool Trigger() { return false; }
        /// <summary>
        /// 曝光
        /// </summary>
        /// <returns></returns>
        public virtual float Exposure { get; set; }
        /// <summary>
        /// 增益
        /// </summary>
        /// <returns></returns>
        public virtual float Gain { get; set; }
        /// <summary>
        /// 帧率
        /// </summary>
        /// <returns></returns>
        public virtual float FrameRate { get; set; }
        /// <summary>
        /// 伽马
        /// </summary>
        public virtual float Gamma { get; set; }

        /// <summary>
        /// 相机数量
        /// </summary>
        /// <returns></returns>
        public virtual List<string> GetCamera() { return new List<string>(); }

        public void SaveImage(ResultType imageType = ResultType.None, string fileName = null)
        {
            string pathImage = string.Empty;
            if (string.IsNullOrEmpty(fileName))
            {
                pathImage = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{name}\Image\{DateTime.Now.ToString("yyyy-MM-dd")}\{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.bmp");
                if (imageType == ResultType.NG)
                {
                    pathImage = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{name}\Image\NG\{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.bmp");
                }
            }
            else
            {
                pathImage = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{name}\{fileName}");
            }
            string dirImage = Path.GetDirectoryName(pathImage);      //检查目录
            if (!Directory.Exists(dirImage))
                Directory.CreateDirectory(dirImage);

            //HWindow.Image.WriteImage("bmp", 0, pathImage);
        }

        public List<ViewWindow.Model.ROI> Roi = new List<ViewWindow.Model.ROI>();

        public HImage Image { get; set; }
    }
}
